package we.extension;

import java.util.List;

import java.nio.channels.SocketChannel;


import we.common.CCommonHandler;

import we.core.bean.CDataContainer;
import we.core.bean.CUser;

import org.json.JSONException;
import org.json.JSONObject;


public class CExtensionUtility {
    private static CExtensionUtility _instance = null;
    private static final Object lock = new Object();

    private CExtensionUtility() {
    }


    public static CExtensionUtility instance() {
        if (_instance == null) {
            synchronized (lock) {
                if (_instance == null) {
                    _instance = new CExtensionUtility();
                }
            }
        }
        return _instance;
    }


    public void joinRoom(CUser u, int fromRoomId, int toRoomId,
                         String password) {
        JSONObject data = new JSONObject();
        try {
            data.put("username", u.username);
            data.put("fromRoomId", fromRoomId);
            data.put("toRoomId", toRoomId);
            data.put("password", password);
            CDataContainer.instance().getCoreHandler("joinRoom").handleEvent(u.channel,
                                                                            data);
        } catch (JSONException e) {
            CCommonHandler.writeErrLog(e);
            // TODO
        }
    }
    
    public void outRoom(CUser u , int fromRoomId){
        JSONObject data = new JSONObject();
        try{
            data.put("username",u.username);
            data.put("fromRoomId",fromRoomId);
            CDataContainer.instance().getCoreHandler("outRoom").handleEvent(u.channel,data);
        }catch (JSONException e){
            CCommonHandler.writeErrLog(e);
        }
    }

    public int createRoom(CUser owner, String rName, int maxUser,
                          String password) {
        JSONObject data = new JSONObject();
        try {
            data.put("owner", owner.username);
            data.put("rName", rName);
            data.put("maxUser", maxUser);
            data.put("password", password);
            SocketChannel channel = owner.channel;
            return CDataContainer.instance().getCoreHandler("createRoom").handleEvent(channel,
                                                                                     data);
        } catch (JSONException e) {
            CCommonHandler.writeErrLog(e);
            return -1; // xay ra loi

            // TODO
        }

    }

    public void disconnectUser(CUser u) {
        JSONObject data = new JSONObject();
        try {
            data.put("username", u.username);
            CDataContainer.instance().getCoreHandler("disconnect").handleEvent(u.channel,
                                                                              data);
        } catch (JSONException e) {
            CCommonHandler.writeErrLog(e);
            // TODO
        }

    }

    public void login(String username, String password) {
        JSONObject data = new JSONObject();
        try {
            data.put("username", username);
            data.put("password", password);
            CDataContainer.instance().getCoreHandler("login").handleEvent(null,
                                                                         data);
        } catch (JSONException e) {
            CCommonHandler.writeErrLog(e);
            // TODO
        }
    }

    public void deleteRoom(int roomId) {
        JSONObject data = new JSONObject();
        try {
            data.put("roomId", roomId);
            CDataContainer.instance().getCoreHandler("deleteRoom").handleEvent(null,
                                                                              data);
        } catch (JSONException e) {
            CCommonHandler.writeErrLog(e);
            // TODO
        }
    }
}
